<template>
  <div class="dashboard-container body-min-width">
    <h2 class="head-big-title">Pelanggan
      <el-button
        round
        type="info"
        size="medium"
        style="float: right;margin-right: 5%"
        @click="deleteArray()">Padam</el-button>
      <el-button
        round
        type="warning"
        size="medium"
        icon="el-icon-plus"
        style="float: right;margin-right: 30px;"
        @click="$router.push({path: '/Customers/New'})">
        Cipta Dealer Utama</el-button>
    </h2>
    <Pathnavigation style="margin-left: 3%;"></Pathnavigation>
    <div style="width: 92%;height: 120px;margin: 20px 4% 0 4%;padding: 10px 0 0 20px;border-radius: 10px;background-color: white;">
      <el-form label-position="top" label-width="80px" :inline="true" class="head-search" :model="searchData" style="float: left;text-align: left;">
        <el-form-item label="Nama Dealer" width="188">
          <el-input v-model="searchData.dealerName" @blur="getResetLoadData" class="input-width" placeholder="Nama"></el-input>
        </el-form-item>
        <el-form-item label="Status" width="188">
          <el-select v-model="searchData.state" @change="getResetLoadData" class="input-width" placeholder="Status">
            <el-option label="Semua" value=""></el-option>
            <el-option label="Mohon" value="0"></el-option>
            <el-option label="Lulus" value="1"></el-option>
            <el-option label="Dilarang" value="2"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <el-table
      v-loading="loading"
      :data="tableData"
      style="width: 92%;margin: 20px 4% 0 4%;padding-left: 10px;border-radius: 20px"
      @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column label="Logo" width="100">
        <template slot-scope="scope">
          <el-image
            style="width: 64px; height: 64px"
            :src="picbase + scope.row.logoUrl"></el-image>
        </template>
      </el-table-column>
      <el-table-column prop="dealerName" label="Nama Dealer" align="center"></el-table-column>
      <el-table-column prop="registCode" label="Nombor Pendaftaran"></el-table-column>
      <el-table-column prop="phoneNumber" label="Nombor Telefon"></el-table-column>
      <el-table-column prop="state" label="Status">
        <template slot-scope="scope">
          <p v-if="scope.row.state == 0">Pending</p>
          <p v-else-if="scope.row.state == 1">Approved</p>
          <p v-else-if="scope.row.state == 2">Rejected</p>
          <p v-else>{{ scope.row.state }}</p>
        </template>
      </el-table-column>
      <el-table-column label="Tindakan" width="166" align="center">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            style="font-size: 26px;padding: 0;background-color: rgb(56,96,134);border: 0;"
            @click="handleEdit(scope.row)"><svg-icon icon-class="ico_view"></svg-icon></el-button>
          <el-button
            type="warning"
            size="mini"
            style="font-size: 26px;padding: 0;background-color: rgb(56,96,134);border: 0;"
            @click="showCustomers(scope.row.id)"><svg-icon icon-class="ico_view_dealer"></svg-icon></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="float: right;margin: 20px 4% 20px 0;"
      layout="prev, pager, next"
      @current-change="handleCurrentChange"
      :current-page="searchData.pageIndex"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { queryDealerApi, deleteDealerApi } from '@/api/user'

export default {
  name: 'Customers',
  computed: {
    ...mapGetters([
      'name'
    ])
  },
  data() {
    return {
      loading: false,
      tableData: [],
      searchData: {
        pageNum: 10,
        pageIndex: 1
      },
      willDel: [],
      orderChart: '',
      total: 0
    }
  },
  mounted() {
    localStorage.removeItem('mainCustomerId')
    this.$removeDetail()
    this.loadData()
  },
  methods: {
    getResetLoadData() {
      this.searchData.pageIndex = 1
      this.loadData()
    },
    showCustomers(id) {
      localStorage.setItem('mainCustomerId', id)
      this.$router.push({ path: '/Customers/Dealer' })
    },
    changeNew() {
      this.$forceUpdate()
    },
    handleCurrentChange(val) {
      this.searchData.pageIndex = val
      this.loadData()
    },
    loadData() {
      this.loading = true
      queryDealerApi(this.searchData).then(res => {
        this.tableData = res.data.records
        this.total = res.data.total
        this.loading = false
      }).catch(err => {
        console.log(err)
        this.loading = false
      })
    }, // 得到数据表格
    handleEdit(row) {
      this.$setDetail(row)
      this.$router.push({ path: '/Customers/Detail' })
    },
    deleteArray() {
      this.$confirm('Padam Data', 'Tips', {
        confirmButtonText: 'Yes',
        cancelButtonText: 'No',
        type: 'warning'
      }).then(() => {
        deleteDealerApi(this.willDel).then(res => {
          this.$message({
            message: 'Berjaya',
            type: 'success'
          })
          this.loadData()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: 'Cancel'
        })
      })
    },
    handleSelectionChange(val) {
      this.willDel = []
      if (val) {
        if (val.length) {
          if (val.length > 0) {
            let newArray = {}
            for (let i = 0; i < val.length; i++) {
              newArray = val[i].id
              this.willDel.push(newArray)
            }
          }
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>
  .head-big-title{
    text-align: left;
    font-size: 20px;
    margin: 20px 0 0 3%;
  }
  .dashboard-container{
    margin-top: 20px;
    text-align: center;
  }

</style>
<style scoped>
  /deep/.head-search label{
    line-height: 30px;
  }
</style>
